package cn.tedu.mall.search.test;

import java.util.Arrays;

import static java.util.Arrays.sort;

public class BubbleSort {
    public static void main(String[] args) {
        int[] a = {21, 31, 23, 8, 12, 3, 81, 20, 939, 19, 23, 91, 9, 2};
        System.out.println(Arrays.toString(a));
        sort(a);
        System.out.println(Arrays.toString(a));
    }

    private static void sort(int[] a) {
        for (int i = a.length - 1; i >= 0; i--) {
            boolean flag = false;
            for (int j = 0; j < i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag){
                break;
            }
            System.out.println(Arrays.toString(a));
        }
        System.out.println(Arrays.toString(a));
    }
}
